package com.hkbigdata.watermark;

import com.hkbigdata.entry.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * @author liuanbo
 * @creat 2023-05-10-17:58
 * @see 2194550857@qq.com
 * ws_1001,1577844001,45
 * ws_1001,1577844003,46
 * ws_1001,1577844007,100
 * ws_1001,1577844003,46
 * ws_1001,1577844002,2
 * ws_1001,1577844008,2
 * ws_1001,1577844003,46
 * ws_1001,1577844009,1
 * ws_1001,1577844002,2
 */
public class Flink01_Window_EventTimeTumbling {
    public static void main(String[] args) throws Exception {
        //1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> waterDS = env.socketTextStream("hadoop102", 9999).
                map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0],
                                Long.parseLong(split[1]),
                                Integer.parseInt(split[2])
                        );
                    }
                });

        //1.watermark 策略
/*        WatermarkStrategy<WaterSensor> waterSensorWatermarkStrategy = WatermarkStrategy.<WaterSensor>forMonotonousTimestamps().withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
            @Override
            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                return element.getTs() * 1000L;
            }
        });*/

        //2.允许乱序数据

        WatermarkStrategy<WaterSensor> waterSensorWatermarkStrategy = WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(2)).withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
            @Override
            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
//                System.out.println(recordTimestamp);
                return element.getTs() * 1000L;
            }
        });

        //2.分配watermark
        //3.1滚动窗口
        SingleOutputStreamOperator<WaterSensor> vs = waterDS.assignTimestampsAndWatermarks(waterSensorWatermarkStrategy)
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(2))
                .sideOutputLateData(new OutputTag<WaterSensor>("Side") {
                })
                .sum("vc");
        //3.2滑动窗口
/*        SingleOutputStreamOperator<WaterSensor> vs = waterDS.assignTimestampsAndWatermarks(waterSensorWatermarkStrategy)
                .keyBy(WaterSensor::getId)
                .window(SlidingEventTimeWindows.of(Time.seconds(5), Time.seconds(2)))
                .sum("vc");*/

        //3.3会话窗口
/*        SingleOutputStreamOperator<WaterSensor> vc = waterDS.assignTimestampsAndWatermarks(waterSensorWatermarkStrategy)
                .keyBy(WaterSensor::getId)
                .window(EventTimeSessionWindows.withGap(Time.seconds(5)))
                .sum("vc");*/

        vs.print("主流");
        vs.getSideOutput(new OutputTag<WaterSensor>("Side") {
        }).print("侧输出流");

        env.execute();


    }
}
